package com.lczc.soa.service;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.lczc.soa.domain.Area;
import com.lczc.soa.domain.AreaExample;
import com.lczc.soa.domain.SearchAreaResponse;
import com.lczc.soa.mapper.AreaMapper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class AreaServiceImpl implements AreaService {

    @Autowired
    private AreaMapper areaMapper;

    @Override
    public int saveArea(Area area) {
//        System.out.println("Service:"+area.getAreaName());
        if(StringUtils.isNotBlank(area.getAreaId())){
            return areaMapper.updateByPrimaryKey(area);

        }else {
            return areaMapper.insert(area);
        }
    }

    @Override
    public SearchAreaResponse findAllArea(Integer pageNum,Integer pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        List<Area> areas = areaMapper.selectByExample(new AreaExample());
        return new SearchAreaResponse(((Page)areas).getTotal(),areas);
    }

    @Override
    public Area findAreaByPrimaryKey(String id) {
        return areaMapper.selectByPrimaryKey(id);
    }

    @Override
    public int deleteArea(String id) {
        return areaMapper.deleteByPrimaryKey(id);
    }

    @Override
    public SearchAreaResponse findAreaLikeName(String keywords, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        List<Area> areas = areaMapper.selectByLikeName(keywords);
        return new SearchAreaResponse(((Page)areas).getTotal(),areas);
    }

    @Override
    public List<Area> listArea() {
        return areaMapper.selectAll();
    }

    @Override
    public Area findAreaByStreet(String id) {
        return areaMapper.selectByStreet(id);
    }
}
